package com.yellowpage.bigdata.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.bigdata.data.TaO2oChlDay;
import com.yellowpage.bigdata.service.TaO2oChlDayService;
import com.yellowpage.white.struts.BaseAction;

public class TaO2oChlDayAction extends BaseAction {

	public String RESULT_QUERY = "query";
//	public String RESULT_ADD = "add";
//	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private TaO2oChlDay taO2oChlDay;
	private TaO2oChlDayService taO2oChlDayService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_id;
	private String query_yesUv;
	private String query_uv;
	private String query_yesPv;
	private String query_dayId;
	private String query_o2oId;
	private String query_channelNo;
	private String query_totalPv;
	private String query_etlTime;
	private String query_totalUv;
	private String query_pv;

	private List<TaO2oChlDay> taO2oChlDayList = new ArrayList<TaO2oChlDay>();
	private List<Map> resultList = new ArrayList<Map>();
	private Map TaO2oIdMap = new HashMap();
	private Map TaO2oChlIdMap = new HashMap();
	
	public void prepare() {
		super.prepare();
		//加载O2O_ID O2O_CHANNEL_ID
		TaO2oIdMap = taO2oChlDayService.getTaO2oIdMap();
		TaO2oChlIdMap = taO2oChlDayService.getTaO2oChannelMap();
		
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.taO2oChlDay = taO2oChlDayService.getTaO2oChlDay(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map<String,String> queryMap = new HashMap<String,String>();	

		queryMap.put("dayId", query_dayId);
		queryMap.put("o2oId", query_o2oId);
		queryMap.put("channelNo", query_channelNo);
		queryMap.put("etlTime", query_etlTime);

		resultList = taO2oChlDayService.queryTaO2oChlDayByPage(queryMap,
				pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public void exportExcel() {
		Map queryMap = new HashMap();
		queryMap.put("dayId", query_dayId);
		queryMap.put("o2oId", query_o2oId);
		queryMap.put("channelNo", query_channelNo);
		queryMap.put("etlTime", query_etlTime);
		resultList = taO2oChlDayService.queryTaO2oChlDayByPage(queryMap);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-download");
		OutputStream outp = null;
		//文件下载，指定默认名
		response.addHeader("Content-Disposition", "attachment;filename="
				+ "O2OChl.xls");
		try {
			outp = response.getOutputStream();
			//创建一个新的excel
			HSSFWorkbook workbook = new HSSFWorkbook();
			//创建sheet页
			HSSFSheet sheet = workbook.createSheet("O2OChl");
			//自适应列宽度
			sheet.autoSizeColumn(1);
			//单元格格式
			HSSFCellStyle cellStyle = workbook.createCellStyle();
			//设置右边框
			cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); 
			//水平居中
			cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
			//设置第一行
			HSSFRow row = sheet.createRow(0);
			String[] title = {"日期","O2O_ID ","O2O名称（备注） ","渠道编号","渠道名称 ","UV（昨日）imei","UV（今日） imei","PV（昨日）请求数","PV（今日） 请求数","累计Uv","累计Pv"};
			
			for(int i = 0 ; i < title.length ; i++) {
				HSSFCell cell = row.createCell(i);
				cell.setCellValue(title[i]);
			}
			for(int i = 0 ; i < resultList.size() ; i++) {
				
				HSSFRow data_row = sheet.createRow(i + 1);
				Map result = resultList.get(i);
				data_row.createCell(0).setCellValue(
						result.get("DAY_ID") != null ? result.get(
								"DAY_ID").toString() : "");
				data_row.createCell(1).setCellValue(
						result.get("O2O_ID") != null ? result.get(
								"O2O_ID").toString() : "");
				data_row.createCell(2).setCellValue(
						result.get("O2O_NAME") != null ? result.get(
								"O2O_NAME").toString() : "");
				data_row.createCell(3).setCellValue(
						result.get("CHANNEL_NO") != null ? result.get(
								"CHANNEL_NO").toString() : "");
				data_row.createCell(4).setCellValue(
						result.get("CHANNEL_NAME") != null ? result.get(
								"CHANNEL_NAME").toString() : "");
				data_row.createCell(5).setCellValue(
						result.get("YES_UV") != null ? result.get(
								"YES_UV").toString() : "");
				data_row.createCell(6).setCellValue(
						result.get("UV") != null ? result.get(
								"UV").toString() : "");
				data_row.createCell(7).setCellValue(
						result.get("YES_PV") != null ? result.get(
								"YES_PV").toString() : "");
				data_row.createCell(8).setCellValue(
						result.get("PV") != null ? result.get(
								"PV").toString() : "");
				data_row.createCell(9).setCellValue(
						result.get("TOTAL_UV") != null ? result.get(
								"TOTAL_UV").toString() : "");
				data_row.createCell(10).setCellValue(
						result.get("TOTAL_PV") != null ? result.get(
								"TOTAL_PV").toString() : "");
			}
			workbook.write(outp);
			response.flushBuffer();
			outp.flush();
			response.reset();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 生成文件错误 !");
		} finally {
			if (outp != null) {
				try {
					outp.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setTaO2oChlDay(TaO2oChlDay taO2oChlDay) {
		this.taO2oChlDay = taO2oChlDay;
	}

	public TaO2oChlDay getTaO2oChlDay() {
		return taO2oChlDay;
	}

	public void setTaO2oChlDayService(TaO2oChlDayService taO2oChlDayService) {
		this.taO2oChlDayService = taO2oChlDayService;
	}

	public List<TaO2oChlDay> getTaO2oChlDayList() {
		return taO2oChlDayList;
	}

	public void setTaO2oChlDayList(List<TaO2oChlDay> taO2oChlDayList) {
		this.taO2oChlDayList = taO2oChlDayList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_id() {
		return this.query_id;
	}

	public void setQuery_id(String id) {
		this.query_id = id;
	}

	public String getQuery_yesUv() {
		return this.query_yesUv;
	}

	public void setQuery_yesUv(String yesUv) {
		this.query_yesUv = yesUv;
	}

	public String getQuery_uv() {
		return this.query_uv;
	}

	public void setQuery_uv(String uv) {
		this.query_uv = uv;
	}

	public String getQuery_yesPv() {
		return this.query_yesPv;
	}

	public void setQuery_yesPv(String yesPv) {
		this.query_yesPv = yesPv;
	}

	public String getQuery_dayId() {
		return this.query_dayId;
	}

	public void setQuery_dayId(String dayId) {
		this.query_dayId = dayId;
	}

	public String getQuery_o2oId() {
		return this.query_o2oId;
	}

	public void setQuery_o2oId(String o2oId) {
		this.query_o2oId = o2oId;
	}

	public String getQuery_channelNo() {
		return this.query_channelNo;
	}

	public void setQuery_channelNo(String channelNo) {
		this.query_channelNo = channelNo;
	}

	public String getQuery_totalPv() {
		return this.query_totalPv;
	}

	public void setQuery_totalPv(String totalPv) {
		this.query_totalPv = totalPv;
	}

	public String getQuery_etlTime() {
		return this.query_etlTime;
	}

	public void setQuery_etlTime(String etlTime) {
		this.query_etlTime = etlTime;
	}

	public String getQuery_totalUv() {
		return this.query_totalUv;
	}

	public void setQuery_totalUv(String totalUv) {
		this.query_totalUv = totalUv;
	}

	public String getQuery_pv() {
		return this.query_pv;
	}

	public void setQuery_pv(String pv) {
		this.query_pv = pv;
	}
	
	public Map getTaO2oIdMap() {
		return TaO2oIdMap;
	}

	public void setTaO2oIdMap(Map taO2oIdMap) {
		TaO2oIdMap = taO2oIdMap;
	}

	public Map getTaO2oChlIdMap() {
		return TaO2oChlIdMap;
	}

	public void setTaO2oChlIdMap(Map taO2oChlIdMap) {
		TaO2oChlIdMap = taO2oChlIdMap;
	}
}
